package com.zhonggong.readDatabase;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;



public class DownThread<pk extends Object> extends Thread {// 进行下载的线程

	private List<pk> list = null;// 数据表子集
	private String excelName;// excel表表名
	private int partSize;// 下载部分的大小
	private int startPos;// 存储的开始位置

	public DownThread(List<pk> list, String excelName, int partSize, int startPos) {
		this.list = list;
		this.excelName = excelName;
		this.partSize = partSize;
		this.startPos = startPos;
	}

	public void run() {
		try{
			InputStream fi = new FileInputStream(excelName);
			System.out.println("fi is null");
			
			XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(fi);//.............................................
				System.out.println("wb is null");
			XSSFSheet sheet0 = wb.getSheetAt(0);
			Iterator it = list.iterator();
			for (int i = startPos; i < (startPos + partSize); i++) 
			{
				Star star = (Star) it.next();
				System.out.println(Thread.currentThread().getName() + " "+ star);
				Row row = sheet0.createRow(i);
				row.createCell(0).setCellValue(star.getName());
				row.createCell(1).setCellValue(star.getSex());
				row.createCell(2).setCellValue(star.getNationality());
			}
			OutputStream fo = new FileOutputStream(excelName);
			System.out.println("fo is null");
			wb.write(fo);
			fi.close();
			fo.close();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
